package cn.gson.springboot.model.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.DrugPurchaseYkDAO;
import cn.gson.springboot.model.dao.DrugsYkDao;
import cn.gson.springboot.model.dao.DrugtypesYkDAO;
import cn.gson.springboot.model.dao.InventoryYkDAO;
import cn.gson.springboot.model.dao.PurchaseDetailsYkDAO;
import cn.gson.springboot.model.mapper.DrugTypesYkMapper;
import cn.gson.springboot.model.mapper.DrugsYkMapper;
import cn.gson.springboot.model.mapper.InventoryYkMapper;
import cn.gson.springboot.model.mapper.YaokurukuxqMapper;
import cn.gson.springboot.model.pojos.DrugPurchaseYk;
import cn.gson.springboot.model.pojos.DrugTypesYk;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.FactoryYk;
import cn.gson.springboot.model.pojos.InventoryYk;
import cn.gson.springboot.model.pojos.PurchaseDetailsYk;
import cn.gson.springboot.model.pojos.ReturnDetailsYk;
import cn.gson.springboot.model.pojos.ReturnDrugsYk;
import cn.gson.springboot.model.vo.InventoryYkVO;
import cn.gson.springboot.model.vo.ReturnDrugsYkVO;
import cn.gson.springboot.model.vo.RukuVo;

//药品service
@Service
@Transactional(rollbackFor=Exception.class)
public class DrugsYkService {
	@Autowired
	private DrugsYkDao hdao;	
	@Autowired
	private DrugsYkMapper dy;
	@Autowired
	private DrugTypesYkMapper dtm;	
	@Autowired
	private PurchaseDetailsYkDAO pddao;
	@Autowired
	private DrugPurchaseYkDAO podao;
	@Autowired
	private DrugtypesYkDAO tyd;
	@Autowired
	private InventoryYkMapper im;
	@Autowired
	private InventoryYkDAO ivd;//库存
	@Autowired
	private YaokurukuxqMapper ykxqm;
	
	//查询所有药品
	public JSONArray findAll(){
		return dy.findAll();
	}
	
	//查所有药品
	public JSONArray findyaopin() {
		return dy.findyaopin();
	}
	
	//采购入库
	public JSONArray drugList() {
		return dy.drugList();
	}
	
	//查询所有药品类型
	public List<DrugTypesYk> findlxing(){
		return dtm.findlxing();
	}
	
	//药品新增
	public void xinzeng(DrugsYk d) {
		 hdao.save(d);
	}
	
	//根据供应商查询药品
	public JSONArray findbyidgys(Integer factoryId){
		JSONArray findbyidgys = dy.findbyidgys(factoryId);
		return findbyidgys;
	}
	
	//根据药品名称、药品规格、供应商名称查询药品
	public JSONArray mcgggys(String drug_name,String drug_specifications,Integer factory_id) {
		return dy.mcgggys(drug_name, drug_specifications, factory_id);
	}
	
	//采购入库主从表新增
	public void saveOrder(RukuVo purVo) {
		//获取采购订单进行新增
		DrugPurchaseYk order = purVo.getCaigou();
		order.setPurchaseStatic("未审核");
		order.setPurchaseTime(new Date());
			
		DrugPurchaseYk save = podao.save(order);
		
		List<PurchaseDetailsYk> details = purVo.getCaigoudetails();
		//循环
		for (PurchaseDetailsYk pur : details) {
			//添加订单信息
			pur.setDrugPurchaseYk(order);
			//查询药品
			DrugsYk dd = hdao.findById(pur.getDrugsYk().getDrugId()).get();
			//重新赋值一下药品对象
			pur.setDrugsYk(dd);
			pddao.save(pur);
		}
	}
	
	//查询id
	public DrugTypesYk bylxid(Integer drugTypesId) {
			DrugTypesYk bylxid=tyd.bylxid(drugTypesId);
			return bylxid;
	}	
	
	//根据药品id查询药品px
	public  DrugsYk findIDdrug(Integer drugId) {
		DrugsYk drugsYk = hdao.findById(drugId).get();
		return drugsYk;
	}
		

	//根据批次改库存数量
	public void saveOrdera(InventoryYkVO purVo) {	
		List<InventoryYk> repertory = purVo.getInven();		
		for(InventoryYk rpurs : repertory ) {
			//查询药品
			JSONArray aa = ykxqm.findpici(rpurs.getDrugPc(),rpurs.getDrugsYk().getDrugId());
			
			if(aa.size()==0) {//如果药库没有此药就加				
				DrugsYk drug = hdao.findById(rpurs.getDrugsYk().getDrugId()).get();
				rpurs.setDrugTime(new Date());
				rpurs.setDrugsYk(drug);
				ivd.save(rpurs);			
			}else  {//如果存在此药就加数量
				//获取JSONArray的ID
				Integer inventory_yk_id = aa.getJSONObject(0).getInteger("inventory_yk_id");
				System.out.println("库存编号"+inventory_yk_id);
				//根据这个ID查询对应的对象
				InventoryYk kc = ivd.findById(inventory_yk_id).get();
				//根据对象原有的数量加上vo类中的数量				
				kc.setInventoryNumber(kc.getInventoryNumber()+rpurs.getInventoryNumber());
				ivd.save(kc);
			}			
		}	
	}	
	
}



